<?php
namespace backend\models;

use yii\db\ActiveRecord;

class HouseInfo extends ActiveRecord
{
    const ALREADY_RENT_OUT = 1; //已经租出去的状态

    public static function tableName()
    {
        return '{{%house_info}}';
    }
    public function getWhole()
    {
        return $this->hasOne(Whole::class, ['house_info_id' => 'id']);
    }
    public function getRoomInfos()
    {
        return $this->hasMany(RoomInfo::class, ['house_info_id' => 'id']);
    }

    public static function showHouseDetailInfo($houseId)
    {
        return self::find()->where(['resource_id'=>$houseId])->asArray()->all();
    }
    /**
    * 函数的含义说明
    *
    * @access public
    * @param mixed $arg1 参数一的说明
    * @param mixed $mixed 这是一个混合类型
    * @return array 返回类型
    */
    public static function houseCount($resource_id)
    {
        return self::find()->where(['resource_id'=>$resource_id])->count();
    }
    public static function houseAlreadyToken($resource_id)
    {
        return self::find()->where(['resource_id'=>$resource_id,"status"=>self::ALREADY_RENT_OUT])->count();
    }
    public static function getIdPassResourceId($resource_id)
    {
        return self::find()->select(['id'])->where(['resource_id'=>$resource_id])->scalar();
    }

    /**
     * 创建
     *
     * @access public
     * @param mixed $arg1 参数一的说明
     * @param mixed $mixed 这是一个混合类型
     * @return array 返回类型
     */
    public static function createHouseInfo($data)
    {
        $model = new self();
        $model->setAttributes($data,false);
        if($model->save()) {
            return $model->id;
        }else{
            throw new \Exception('插入失败');
        }
    }
}